<?php
/* File Subsystem */

class System_FS {
									  
									  
//*************************************************************************************************
/**
 * 
 * @param string $fs_root absolute path to the KOHV 'file system' root directory. 
 * @param string $lwd local working directory (cwd within 'file system')
 */
		
		private $lwd;
		private $pathTool;
		
		public function __construct($fsRoot, $lwd = '/') 
		{			
			$this->lwd = $lwd;
			$this->pathTool = new System_FS_PathTool($fsRoot);
		}
		
//*************************************************************************************************
/**
 * 
 * Reads and returns contents of a directory.
 * @param string $localPath path to directory where to read the content from.
 * 
 * @return ['dirs'=>list_of_dirs, 'files'=> list_of_files, 'dir_path'=>path_to_the_dir]
 * 			if ['dirs'] == null and ['files'] == null then there was an error opening ['dir_path'] 
 */
		public function readDir($localPath = "")
		{
	
			
				//echo ("\n\nLWD=".$this->lwd."\n\n" );	
				if ($localPath == "") $localPath = $this->lwd;	
			
				$realDir = $this->pathTool->localToGlobal($localPath);
				
				$dirPath = $this->pathTool->globalToLocal($realDir);
				
				//echo ("\n\nrealDir=".$realDir."\n\n" );
				
				if( $realDir === false )
				{					
					return array('dirs'=>null, 'files'=>null, 'dir_path'=>$localPath, 'msg' => 'Directory does not exist!');
				}
				
				$files = array();
				$dirs = array();
				
				// make list of files and dirs
				$handle = @opendir($realDir); 

				if ($handle===false)
					return array('dirs'=>null, 'files'=>null, 'dir_path'=>$dirPath, 'msg' => 'This is not a directory!');
				
			    while (false !== ($name = readdir($handle))) 
			    {		
			    	$info = stat($realDir . '/' . $name);
			    	$item = array('name'=>$name, 'info'=>$info);	    	
			    	////////////////////////////////////////////////////////
			    	// Sort out files and directories 
			    	if ( is_dir($realDir.'/'.$name) )
			        {		        			        	
			        	// skip adding '..' when path equals root
			        	if ($localPath === '/' && $name === '..') 
			        		continue;
			        								
			        	array_push($dirs,$item);
			        }		        
			        else
			        {		        
			        	array_push($files,$item);
			        }
			        ////////////////////////////////////////////////////////
			        
			    }		    
		    	closedir($handle);
			    	
		    	
		    	
				return array('dirs'=>$dirs, 'files'=> $files, 'dir_path'=>$dirPath, 'msg' => 'ok');
		}
		


//*************************************************************************************************
/**
 * Checks if a directory exists or not.
 * @param $localPath path to a directory in kohf fs
 * @return true if the directory exists, false otherwise.
 */
		public function dirExist($localPath)
		{			
			return  $this->pathTool->dirExist($localPath);
		}
		
//*************************************************************************************************
		public function changeLwd($localPath)
		{
			//echo "\n\nsetLwd(), not implemented!!!\n\n";
			
			// check if path is relative (i.e. does not start with '/'.
			// if it is relative, it should be appended to the local working directory (lwd) to form an absolute path, as PathTool deals with them
			if ($localPath[0] !== '/')
			{
				$localPath = $this->lwd . '/' . $localPath;
			}
				
			if ( $this->pathTool->dirExist($localPath) )
			{
				$realPath = $this->pathTool->localToGlobal($localPath);
				$localPath = $this->pathTool->globalToLocal($realPath);
				$this->lwd = $localPath;
				return  true;
			}
			
			return false;
		}
		
//*************************************************************************************************
		public function getLwd()
		{
			return $this->lwd;
		}
		
//*************************************************************************************************
		public function __toString()
		{
			$res = "\n[FS]\n";
			$res.= "lwd: " . $this->lwd ."\n";
			$res.= $this->pathTool->__toString();
			
			return $res;
		}
		
//*************************************************************************************************
		public function touch($fileName, $path=null)
		{
			if ($path == null)
				$path = $this->lwd;

			$path = $this->pathTool->localToGlobal($path);			
			touch($path.'/'.$fileName);
			/*
			$filzeSize = @filesize($path.'/'.$fileName);
			
			$fp = fopen($path.'/'.$fileName, 'a') or die("can't open file");
			fclose($fp);
			
			// file didn't exist before 
			if ($filzeSize === false)
			{
				return;
			}		
			
			$fp = fopen($path.'/'.$fileName, 'a') or die("can't open file");			
			fwrite($fp, ' ');
			ftruncate($fp, $filzeSize);
			
			fclose($fp);
			*/
			
		}

		public function getRealPath($localPath)
		{
			if ($localPath[0] !== '/')
			{
				$localPath = $this->lwd . '/' . $localPath;
			}
			
			return $this->pathTool->localToGlobal($localPath);
			
		}
		
}